package api;

import org.basex.core.BaseXException;

/**
 * This class returns available statistics.
 *
 * @author Football-Statistics team
 */
public final class Statistics {
    private DatabaseManager dm;
    private Cache cache;

    /**
    * Constructor
    * @param dm DatabaseManager
    */    
    public Statistics(DatabaseManager dm) {
        this.dm = dm;
        cache = new Cache(this.dm);
    }
    
    /**
    * Sets the Database Manager
    * @param dm DatabaseManager
    */
    public void setDatabaseManager(DatabaseManager dm) {
        this.dm = dm;
    }

    /**
    * Gets all played matches of the given league.
    * Contains date, time, teams and score
    * @param lid league id
    * @return well-formed xml String, <b>structure</b>: <a href="http://pastebin.com/qnN9H4Ws">http://pastebin.com/qnN9H4Ws</a>
    * @throws BaseXException on database error
    */
    public String getMatches(String lid) throws BaseXException {
        String xml = "";
        String cacheID = "matches-" + lid;
        xml = this.cache.read(cacheID);
        if (xml.equals("")) {
            xml = "<matches></matches>";
        }
        return xml;
        
        /* - possible server side XSLT transformation
        TransformerFactory factory = TransformerFactory.newInstance();
        Templates templates = factory.newTemplates(new StreamSource(new File("matches.xsl")));
        Transformer transformer = templates.newTransformer();
        //transformer.setParameter("message", "NEW MESSAGE");
        StringReader reader = new StringReader(xml);
        StringWriter writer = new StringWriter();
        transformer.transform(new StreamSource(reader),new StreamResult(writer));
        return writer.toString();
        */
    }

    /**
    * Gets match info
    * @param lid league id
    * @param mid match id
    * @return well-formed xml String, <b>structure</b>: <a href="http://pastebin.com/K3JUjD8S">http://pastebin.com/K3JUjD8S</a>
    * @throws BaseXException on database error
    */
    public String getMatch(String lid, String mid) throws BaseXException {
        return this.dm.queryCollection("collection('"+lid+"')/match[@id='"+mid+"']");
    }
    
    /**
    * Gets actual standings for given league
    * @param lid league id
    * @param hg home/guests/both
    * @param fh full/half
    * @return well-formed xml String, <b>structure</b>: <a href="http://pastebin.com/Q8V9LPLE">http://pastebin.com/Q8V9LPLE</a>
    * @throws BaseXException on database error
    */    
    public String getStandings(String lid, String hg, String fh) throws BaseXException {
        String xml = "";
        String cacheID = "standings-" + hg + "-" + fh + "-" + lid;
        xml = this.cache.read(cacheID);
        if (xml.equals("")) {        
            xml = "<standings></standings>";
        }
        return xml;
    }    

    /**
    * Gets points of all teams for each round
    * @param lid league id
    * @return well-formed xml String, <b>structure</b>: <a href="http://pastebin.com/dpZbb18R">http://pastebin.com/dpZbb18R</a>
    * @throws BaseXException on database error
    */    
    public String getTeamsProgress(String lid) throws BaseXException {
        String xml = "";
        String cacheID = "teams-progress-" + lid;
        xml = this.cache.read(cacheID);
        if (xml.equals("")) {        
            xml = "<teams-progress></teams-progress>";
        }
        return xml;        
    }
    
    /**
    * Gets player statistics for given league
    * @param lid league id
    * @return well-formed xml String, <b>structure</b>: <a href="http://pastebin.com/A4FL0n3W">http://pastebin.com/A4FL0n3W</a>
    * @throws BaseXException on database error
    */ 
    public String getPlayersStatistics(String lid) throws BaseXException{
        String xml = "";
        String cacheID = "players-statistics-" + lid;
        xml = this.cache.read(cacheID);
        if (xml.equals("")) {
            xml = "<playerStatistics></playerStatistics>";
        }
        return xml;
    }
    
    /**
    * Gets matches between two teams for given league
    * @param lid league id
    * @return well-formed xml String, <b>structure</b>: multiple <a href="http://pastebin.com/K3JUjD8S">http://pastebin.com/K3JUjD8S</a> between &lt;matches&gt;&lt;/matches&gt; tag
    * @throws BaseXException on database error
    */    
    public String getMatchesByTeams(String lid, String firstTeam, String secondTeam) throws BaseXException{
        String xml = "";
        String query = "for $doc in collection('" + lid + "')" + 
                       "where $doc//home = '" + secondTeam +"' and $doc//guests = '" + firstTeam +"'"  +
                       "or $doc//home = '" + firstTeam +"' and $doc//guests = '" + secondTeam +"'" +
                       "return $doc";
        xml = dm.queryCollection(query);
        return "<matches>\n" + xml + "\n</matches>";
    }
}
